package com.controller;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.client.OAuth2AuthorizeRequest;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.security.oauth2.core.user.DefaultOAuth2User;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.List;

@RestController
public class HelloController {

    public static final RestTemplate REST_TEMPLATE = new RestTemplate();
    private final OAuth2AuthorizedClientManager oAuth2AuthorizedClientManager;

    public HelloController(OAuth2AuthorizedClientManager oAuth2AuthorizedClientManager) {
        this.oAuth2AuthorizedClientManager = oAuth2AuthorizedClientManager;
    }


    @GetMapping("/")
    public String hello() {
        OAuth2User principal = (OAuth2User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        return "你好：" + principal.getName();
    }

    @GetMapping("/friends/list")
    public List<String> friendsList() {

        OAuth2AuthenticationToken authentication = (OAuth2AuthenticationToken) SecurityContextHolder.getContext().getAuthentication();

        OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId(
                        authentication.getAuthorizedClientRegistrationId()
                )
                .principal(authentication)
                .build();

        OAuth2AuthorizedClient oauth2Authentication = oAuth2AuthorizedClientManager.authorize(authorizeRequest);


//        模拟向资源服务器发起 请求
        String tokenType = oauth2Authentication.getAccessToken().getTokenType().getValue();

        MultiValueMap<String,String> header = new HttpHeaders();


        header.add("Authorization",tokenType+" "+oauth2Authentication.getAccessToken().getTokenValue());

        RequestEntity<Object> requestEntity = new RequestEntity<Object>(

                header,
                HttpMethod.GET,
                UriComponentsBuilder.fromHttpUrl("http://localhost:12000/friends/list" ).build().toUri()

        );

        ResponseEntity<List<String>> exchange = REST_TEMPLATE.exchange(requestEntity, new ParameterizedTypeReference<List<String>>() {
        });
        return exchange.getBody();
    }
}
